// pages/myAttention/myAttention.js
let util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        isshow: true,
        attention: '',
        myattention:[],
        attentionMe:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.type == 2) {
            this.setData({
                isshow: !this.data.isshow
            });
        }
        this.getAttentionMe();
        this.getMyattention();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    change() {
        var type = this.data.isshow;
        if(type){
            this.getAttentionMe();
        }else{
            this.getMyattention();
        }
        this.setData({
            isshow: !this.data.isshow
        })
    },
    //获取我关注的人内容信息
    getMyattention:function(){
        var userinfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var that = this;
        util.wxAjax("admin/follow","post",{
            adminid: userinfo,
            pageNum:1
        },function(res){
            var data = res.data;
            if(!!data.data){
                that.setData({
                    myattention: data.data
                });
            }else{
                that.setData({
                    myattention: []
                });
            }
        })
    },
    //获取关注我的人内容信息
    getAttentionMe:function(){
        var userinfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var that = this;
        util.wxAjax("admin/fans", "post", {
            adminid: userinfo,
            pageNum: 1
        }, function (res) {
            var data = res.data;
            if(!!data.data){
                that.setData({
                    attentionMe: data.data
                });
            }else{
                that.setData({
                    attentionMe: []
                });
            }
        })
    },
    //取消关注
    cancelAttention:function(e){
        var fansid = e.currentTarget.dataset.fansid;
        var userinfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var that = this;
        util.wxAjax("admin/cancelFollow","post",{
            adminid: userinfo,
            fansid: fansid
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                that.getMyattention();
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        })
    },
    //添加关注
    addAttention:function(e){
        var fsid = e.currentTarget.dataset.fsid;
        var userinfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var that = this;
        util.wxAjax("admin/addFollow", "post", {
            adminid: userinfo,
            fansid: fsid
        }, function (res) {
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                that.getAttentionMe();
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        })
    }
})